home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / Wood.0.72 / Sources / WoodDoc_Creation.m < prev    next >
Encoding:
Text File  |  1995-08-11  |  7.8 KB  |  297 lines

  1. #import "wooddoc.h"
  2. #import "ScrollViewDeluxeForOOE.h"
  3. #import "Ruler.h"
  4.  
  5. @implementation WoodDoc (Creation)
  6.  
  7. static const int     classVersion        = 100;        // version number * 100
  8. static NXCoord         origX                 = 300.0;    // doc starting position
  9. static NXCoord         origY                 = 200.0;
  10. static NXCoord        posInc                 = 20.0;        // increment to position
  11. static BOOL            shouldIncPos        = YES;
  12. static int          tagProvider         = 0; 
  13.  
  14. + initialize
  15. {
  16.     if(self == [WoodDoc class]){
  17.         [self setVersion:classVersion];
  18.         [self setDocExtension:localString("wood")];
  19.         [self setDefaultTitleFormat:localString("Untitled%d")];
  20.         [self initializePasteboardTypes];
  21.     }
  22.     return self;
  23. }
  24.  
  25. + setIncPos:(BOOL)aBool
  26. {
  27.     shouldIncPos = aBool;
  28.     return self;
  29. }
  30.  
  31. - initFromFile:(const char *)file
  32. {
  33.     NXTypedStream *stream;
  34.     char *strPtr = rindex(file, '.');
  35.     BOOL shouldRegister = NO;
  36.     
  37.     if(strcmp([[self class] docExtension],++strPtr)) 
  38.         return [self initFromPasteboard:[Pasteboard newByFilteringFile:file]];
  39.     stream = NXOpenTypedStreamForFile(file,NX_READONLY);
  40.     if(stream){
  41.         if([self setUpNib]){    
  42.             [window setTitle:localString("Loading...")];        
  43.             if([self loadTreeFrom:stream]){
  44.                 [self instanceAwake];
  45.                 [self dirty:NO];
  46.                 [self updateViewsDirty:NO rect:NULL flag:(UPDATE_ALL | SCROLL_TREEVIEW)];
  47.                 [self setSavedDocument:YES];
  48.                 [self setEmpty:(tree ? NO : YES)];
  49.                 [self setName:file];
  50.                 [window makeKeyAndOrderFront:self];
  51.                 shouldRegister = YES;
  52.             }
  53.         } else 
  54.             Notify(localString("WoodDoc: can't set up doc nib"),file);
  55.         NXCloseTypedStream(stream);
  56.         if(shouldRegister){
  57.             [NXApp registerDoc:self];
  58.             return self;
  59.         } else {
  60.             [self free];
  61.             return nil;
  62.         }
  63.     } else {
  64.         Notify(localString("WoodDoc: can't open file"),file);
  65.         [self free];
  66.         return nil;
  67.     }
  68. }
  69.  
  70. - initOOEFromFile:(const char *)file
  71. {
  72.     NXTypedStream *stream;
  73.     char *strPtr = rindex(file, '.');
  74.     BOOL shouldRegister = NO;
  75.     
  76.     if(strcmp([[self class] docExtension],++strPtr)) 
  77.         return [self initFromPasteboard:[Pasteboard newByFilteringFile:file]];
  78.     stream = NXOpenTypedStreamForFile(file,NX_READONLY);
  79.     if(stream){
  80.         if([self setUpNib]){    
  81.             [window setTitle:localString("Loading...")];        
  82.             if([self loadTreeFrom:stream]){
  83.                 [self instanceAwake];
  84.                 [self dirty:NO];
  85.                 [self updateViewsDirty:NO rect:NULL flag:(UPDATE_ALL | SCROLL_TREEVIEW)];
  86.                 [self setSavedDocument:YES];
  87.                 [self setEmpty:(tree ? NO : YES)];
  88.                 [self setName:file];
  89.                 shouldRegister = YES;
  90.             }
  91.         } else 
  92.             Notify(localString("WoodDoc: can't set up doc nib"),file);
  93.         NXCloseTypedStream(stream);
  94.         if(shouldRegister){
  95.             [NXApp registerDoc:self];
  96.             return self;
  97.         } else {
  98.             [self free];
  99.             return nil;
  100.         }
  101.     } else {
  102.         Notify(localString("WoodDoc: can't open file"),file);
  103.         [self free];
  104.         return nil;
  105.     }
  106. }
  107.  
  108. - initFromPasteboard:pasteboard
  109. {
  110.     char *stringPos, *tempPtr, *pathList;
  111.     char tempBuf[MAXPATHLEN+1];
  112.     char **filePath = NULL;
  113.     int files = 0, length = 0, count = 0;
  114.     BOOL flag = NO;
  115.     id pBoard = [Pasteboard newByFilteringTypesInPasteboard:pasteboard];
  116.     
  117.     if ([pBoard findAvailableTypeFrom:&NXFilenamePboardType num:1]) {
  118.         if ([pBoard readType:NXFilenamePboardType data:&pathList length:&length]) 
  119.             flag = YES;
  120.     } else if ([pBoard findAvailableTypeFrom:&NXAsciiPboardType num:1]) {
  121.         if ([pBoard readType:NXAsciiPboardType data:&pathList length:&length]) 
  122.             flag = YES;
  123.     }
  124.     if (!flag) {
  125.         Notify(localString("Pasteboard"),
  126.                localString("Could not parse or filter ASCII data from the requested source."));
  127.         return nil;
  128.     }
  129.     stringPos = tempPtr = pathList;
  130.     while (stringPos = index(stringPos, '\t')) {
  131.         count = (int)(stringPos-tempPtr);
  132.         strncpy(tempBuf,tempPtr,count);
  133.         *(tempBuf+count) = '\0';
  134.         filePath = addFile(tempBuf,filePath,files,[self zone]);
  135.         files++;
  136.         stringPos++;
  137.         tempPtr=stringPos;
  138.     }
  139.     count = strlen(tempPtr);
  140.     strncpy(tempBuf,tempPtr,count);
  141.     *(tempBuf+count) = '\0';
  142.     filePath = addFile(tempBuf,filePath,files,[self zone]);
  143.     files++;
  144.     while (files--) {
  145.         if (![[[NXApp docClass] allocFromZone:[NXApp newDocZone]] initFromFile:filePath[files]]) 
  146.             Notify(localString("Pasteboard: can't open file"),filePath[files]);
  147.     }
  148.     freeList(filePath);
  149.     [pBoard deallocatePasteboardData:pathList length:length];
  150.     return self;
  151. }
  152.  
  153. - initNewOOE:client
  154. {
  155.     [self init];
  156.     [self connectToClient:client];
  157.     [self setTitle_OOE:nil];
  158.     return self;
  159. }
  160.  
  161. - initOOE:client
  162. {
  163.     [self initNewOOE:client];
  164.     [self retrieveData:client];
  165.     return self;
  166. }
  167.  
  168. - init
  169. {
  170.     if([self setUpNib]){
  171.         tree = nil;
  172.         printInfo = [DGPrintInfo new];
  173.         [printInfo setMarginLeft:36.0 right:36.0 top:36.0 bottom:36.0];
  174.         [printInfo setHorizCentered:YES];
  175.            [printInfo setVertCentered:YES];
  176.         [self instanceAwake];
  177.         [self dirty:NO];
  178.         [self setSavedDocument:NO];
  179.         [self setEmpty:YES];
  180.         [self setName:NULL andDirectory:NULL];
  181.         [NXApp registerDoc:self];
  182.         [window makeKeyAndOrderFront:self];
  183.         return self;
  184.     } else
  185.         return nil;
  186. }
  187.  
  188. - setUpNib
  189. {
  190.     [NXApp loadLocalNib:"WoodDoc" owner:self];
  191.     return self;
  192. }
  193.  
  194. - instanceAwake
  195. {
  196.     NXRect frameRect;
  197.     
  198.     tag = tagProvider++;
  199.     undoManager = [[UndoManager allocFromZone:[self zone]] init];
  200.     [undoManager addUndoDelegate:self];
  201.     lastSelectedNode = nil;
  202.     findSelectedNode = nil;
  203.     isOOEServer = NO;
  204.     theOOEClient = nil;
  205.     theOOEClientPath = NULL;
  206.     docScale = 1.0;
  207.     showMarker = YES;
  208.     defaultProps.border = 10.0;
  209.     defaultProps.parentDistance = 30.0;
  210.     defaultProps.zipped = NO;
  211.     defaultProps.shadow = YES;
  212.     defaultProps.fill = YES;
  213.     defaultProps.outline = YES;
  214.     defaultProps.font = [Font newFont:"Times-Roman" size:12.0];
  215.     defaultProps.pathKind = RECT_NODETYPE;
  216.     defaultProps.linkKind = 1;
  217.     defaultProps.biegFactor = 0.33;
  218.     defaultProps.fillColor = NX_COLORWHITE;
  219.     defaultProps.outlineColor = NX_COLORBLACK;
  220.     defaultProps.textColor = NX_COLORBLACK;
  221.     defaultProps.shadowColor = NX_COLORLTGRAY;
  222.     defaultProps.defaultNodeName = NXCopyStringBuffer("Node");
  223.     defaultProps.ending = ENDING_NONE;
  224.     defaultProps.parentEnding = ENDING_NONE;
  225.     [window moveTo:origX :origY];
  226.     if(shouldIncPos){
  227.         origX += posInc; origY -= posInc;
  228.         if(origX > 700) 
  229.             origX = 110;
  230.         if(origY < 0) 
  231.             origY = 70;
  232.     }
  233.     [window setDelegate:self];
  234.     calcFrame(printInfo,&frameRect);
  235.     treeView = [[TreeView allocFromZone:[window zone]] initFrame:&frameRect];
  236.     [treeView setAgent:self];
  237.     NXSetRect(&frameRect,0,0,100,100);
  238.     scrollView = [[ScrollViewDeluxeForOOE allocFromZone:[window zone]] initFrame:&frameRect];
  239.     [scrollView setDocView:treeView];
  240.     [scrollView setZoomButtonVisible:YES];
  241.     [scrollView setRulerClass:[Ruler class]];
  242.     [scrollView hideRulers:self];
  243.     [scrollView setPageLeftRightButtonsVisible:NO];
  244.     [scrollView setPageUpDownButtonsVisible:NO];
  245.     [textView removeFromSuperview];
  246.     [[textView docView] setDelegate:self];       
  247.        [doubleView addSubview:scrollView];
  248.     [doubleView addSubview:textView];
  249.     [doubleView display];
  250.     [window makeFirstResponder:treeView];
  251.     textViewSaved = YES;
  252.     saveToAccessoryWindow = nil;
  253.     saveToAccessoryControl = nil;
  254.     return self;
  255. }
  256.  
  257. - loadTreeFrom:(NXTypedStream *)stream
  258. {
  259.     id ret = self;
  260.     NX_DURING
  261.         tree = NXReadObject(stream);
  262.         [tree setDelegateRecursive:self];
  263.         printInfo = NXReadObject(stream);
  264.     NX_HANDLER
  265.         switch(NXLocalHandler.code){
  266.             case TYPEDSTREAM_CALLER_ERROR: 
  267.                     NXRunAlertPanel(0,"Typed stream caller error",0,0,0);
  268.                     ret = nil;
  269.                     break;
  270.             case TYPEDSTREAM_FILE_INCONSISTENCY:
  271.                     NXRunAlertPanel(0,"Typed stream file inconsistency",0,0,0);
  272.                     ret = nil;
  273.                     break;
  274.             case TYPEDSTREAM_CLASS_ERROR:
  275.                     NXRunAlertPanel(0,"Typed stream class error",0,0,0);
  276.                     ret = nil;
  277.                     break;
  278.             case TYPEDSTREAM_TYPE_DESCRIPTOR_ERROR:
  279.                     NXRunAlertPanel(0,"Typed stream descriptor error",0,0,0);
  280.                     ret = nil;
  281.                     break;
  282.             case TYPEDSTREAM_WRITE_REFERENCE_ERROR:
  283.                     NXRunAlertPanel(0,"Typed stream write reference error",0,0,0);
  284.                     ret = nil;
  285.                     break;
  286.             case TYPEDSTREAM_INTERNAL_ERROR:
  287.                     NXRunAlertPanel(0,"Typed stream internal error",0,0,0);
  288.                     ret = nil;
  289.                     break;
  290.         }
  291.     NX_ENDHANDLER
  292.     return ret;
  293. }
  294.  
  295. @end
  296.  
  297.